package com.xiaoyu.net.queue;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.birbit.android.jobqueue.Job;
import com.birbit.android.jobqueue.Params;
import com.birbit.android.jobqueue.RetryConstraint;

import in.srain.cube.request.IRequest;
import in.srain.cube.util.CLog;

public class RequestJob<T> extends Job {

    private static final String TAG = "RequestJob";
    private IRequest<T, ?> mRequest;

    protected RequestJob(Params params, IRequest<T, ?> request) {
        super(params);
        mRequest = request;
    }

    @Override
    public void onAdded() {
    }

    @Override
    public void onRun() throws Throwable {
        try {
            mRequest.requestSync();
        } catch (Exception ex) {
            ex.printStackTrace();
            throw ex;
        }
    }

    @Override
    protected void onCancel(int i, @Nullable Throwable throwable) {
        CLog.w(TAG, "onCancel: %s %s", mRequest.isPostToMainThread(), mRequest.getRequestData());
    }

    @Override
    protected RetryConstraint shouldReRunOnThrowable(@NonNull Throwable throwable, int i, int i1) {
        CLog.w(TAG, "shouldReRunOnThrowable: %s %s %s", throwable, mRequest.isPostToMainThread(), mRequest.getRequestData());
        return RetryConstraint.CANCEL;
    }
}
